/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f4xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "UPRE_LOCATION.h"
#include "UPRE_Decision.h"
#include "UPRE_ROBOT_TASK.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
extern uint8_t USART1_RX[100];
extern uint8_t g_Auto_Num ;
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_adc1;
extern ADC_HandleTypeDef hadc1;
extern CAN_HandleTypeDef hcan1;
extern TIM_HandleTypeDef htim3;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern TIM_HandleTypeDef htim8;

/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/******************************************************************************/
/* STM32F4xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f4xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles EXTI line4 interrupt.
  */
void EXTI4_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI4_IRQn 0 */

  /* USER CODE END EXTI4_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(Remake_Pin);
  /* USER CODE BEGIN EXTI4_IRQn 1 */

  /* USER CODE END EXTI4_IRQn 1 */
}

/**
  * @brief This function handles ADC1, ADC2 and ADC3 global interrupts.
  */
void ADC_IRQHandler(void)
{
  /* USER CODE BEGIN ADC_IRQn 0 */

  /* USER CODE END ADC_IRQn 0 */
  HAL_ADC_IRQHandler(&hadc1);
  /* USER CODE BEGIN ADC_IRQn 1 */

  /* USER CODE END ADC_IRQn 1 */
}

/**
  * @brief This function handles CAN1 RX0 interrupts.
  */
void CAN1_RX0_IRQHandler(void)
{
  /* USER CODE BEGIN CAN1_RX0_IRQn 0 */

  /* USER CODE END CAN1_RX0_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan1);
  /* USER CODE BEGIN CAN1_RX0_IRQn 1 */

  /* USER CODE END CAN1_RX0_IRQn 1 */
}

/**
  * @brief This function handles TIM3 global interrupt.
  */
void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */

  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */

  /* USER CODE END TIM3_IRQn 1 */
}

/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */

  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
    if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)!=RESET) {
        __HAL_UART_CLEAR_IDLEFLAG(&huart1);
        HAL_UART_DMAStop(&huart1);
        static uint8_t Sum_W = 0;
        static uint8_t Sum_H = 0;
//        len = 18 - __HAL_DMA_GET_COUNTER(huart6.hdmarx);
        HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
        /*********************************************************深度?��??******�????��??????*********************************************************************/
        for (int i = 1; i < 17; i++) {
            Sum_W+=USART1_RX[i];
        }
        if(USART1_RX[0] == 0x29 && USART1_RX[18] == 0xef && Sum_W == USART1_RX[17]) {
            Percept_RePack.TargetBall.Target_ball_x = ((float)((int)USART1_RX[1] | (int)(USART1_RX[2] << 8) | (int)(USART1_RX[3] << 16) | (int)(USART1_RX[4] << 24))) * 0.001f;
            Percept_RePack.TargetBall.Target_ball_y = ((float)((int)USART1_RX[5] | (int)(USART1_RX[6] << 8) | (int)(USART1_RX[7] << 16) | (int)(USART1_RX[8] << 24))) * 0.001f;
            Percept_RePack.TargetBall.Target_ball_depth = ((float)((int)USART1_RX[9] | (int)(USART1_RX[10] << 8) | (int)(USART1_RX[11] << 16) | (int)(USART1_RX[12] << 24))) * 0.001f;
            Percept_RePack.TargetBall.Target_ball_angle = ((float)((int)USART1_RX[13] | (int)(USART1_RX[14] << 8) | (int)(USART1_RX[15] << 16) | (int)(USART1_RX[16] << 24))) * 0.001f;
            if((Percept_RePack.TargetBall.Target_ball_x != 0) && (Percept_RePack.TargetBall.Target_ball_y != 0) && (Percept_RePack.TargetBall.Target_ball_depth != 0) && (Percept_RePack.TargetBall.Target_ball_angle != 0))
                Chassis_Sense_Bool.Start_Find_Ball = true;
        }
        Sum_W = 0;
//        printf("X:%f\tY:%f\tde:%f\ttAn:%f\r\n",Percept_RePack.TargetBall.Target_ball_x,Percept_RePack.TargetBall.Target_ball_y,Percept_RePack.TargetBall.Target_ball_depth,Percept_RePack.TargetBall.Target_ball_angle);
        /*********************************************************�???谷�?*******�???????谷�?*******************************************************************/
        static union{
            float value;
            uint8_t P[4];
        }Target_Ball_Rx;
        static union{
            int32_t value;
            uint8_t P[4];
        }uint8_To_int32;

        for (int j = 1; j < 17; j++) {
            Sum_H+=USART1_RX[j];
        }
//        printf("OKKKKKKKKKKKKKKKKKKKKKKK!\r\n");

/*****************************************MA*********************************************************/
        if (USART1_RX[0] == 0xAA && USART1_RX[18] == 0xA1 && USART1_RX[17] == Sum_H)
        {
            if(USART1_RX[6] == 0x03 ){
                Percept_RePack.DivideBall.colour = purplr_ball;
                Timer_Stamp.TimeStamp_is_update_color=0;
                USART1_RX[6] = 0x05;

            }
            if(USART1_RX[6] == 0x01)
            {
                Percept_RePack.DivideBall.colour=blue_ball;
                Timer_Stamp.TimeStamp_is_update_color=null;
                Timer_Stamp.TimeStamp_is_stop_receive_ball=null;
                USART1_RX[6] = 0x05;

                Chassis_Sense_Bool.Recv_Blue_Ball = true;
            }

            if(USART1_RX[1] == 0x01)//???��??
            {
                uint8_To_int32.P[0] = USART1_RX[2];
                uint8_To_int32.P[1] = USART1_RX[3];
                uint8_To_int32.P[2] = USART1_RX[4];
                uint8_To_int32.P[3] = USART1_RX[5];
                Chassis_Sense_Value.Which_Goal = uint8_To_int32.value;
//                HAL_UART_Transmit(&huart1, USART1_TX, 39,HAL_MAX_DELAY);  //???��?��?��??�??��??�??

            }
            else if(USART1_RX[1] == 0x02)//红�?��??
            {
                uint8_To_int32.P[0] = USART1_RX[2];
                uint8_To_int32.P[1] = USART1_RX[3];
                uint8_To_int32.P[2] = USART1_RX[4];
                uint8_To_int32.P[3] = USART1_RX[5];
                Chassis_Sense_Value.Which_Goal = uint8_To_int32.value;

//                HAL_UART_Transmit(&huart1, USART1_TX, 39,HAL_MAX_DELAY);  //???��?��?��??�??��??�??
            }
        }
        Sum_H = 0;
        HAL_UART_Receive_DMA(&huart1, USART1_RX, 19);
    }
  /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles USART2 global interrupt.
  */
void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */

  /* USER CODE END USART2_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */

  /* USER CODE END USART2_IRQn 1 */
}

/**
  * @brief This function handles TIM8 update interrupt and TIM13 global interrupt.
  */
void TIM8_UP_TIM13_IRQHandler(void)
{
  /* USER CODE BEGIN TIM8_UP_TIM13_IRQn 0 */

  /* USER CODE END TIM8_UP_TIM13_IRQn 0 */
  HAL_TIM_IRQHandler(&htim8);
  /* USER CODE BEGIN TIM8_UP_TIM13_IRQn 1 */

  /* USER CODE END TIM8_UP_TIM13_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream0 global interrupt.
  */
void DMA2_Stream0_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream0_IRQn 0 */

  /* USER CODE END DMA2_Stream0_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_adc1);
  /* USER CODE BEGIN DMA2_Stream0_IRQn 1 */

  /* USER CODE END DMA2_Stream0_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream2 global interrupt.
  */
void DMA2_Stream2_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream2_IRQn 0 */

  /* USER CODE END DMA2_Stream2_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_usart1_rx);
  /* USER CODE BEGIN DMA2_Stream2_IRQn 1 */

  /* USER CODE END DMA2_Stream2_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream7 global interrupt.
  */
void DMA2_Stream7_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream7_IRQn 0 */

  /* USER CODE END DMA2_Stream7_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_usart1_tx);
  /* USER CODE BEGIN DMA2_Stream7_IRQn 1 */

  /* USER CODE END DMA2_Stream7_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
